Changing the active culture in ASP.NET

In the previous chapter, we had a look at the Culture Info class, and we have briefly discussed how to change it for a page. However, since the current culture has changed, it is necessary, in this chapter there will be a deeper look of the different ways of accomplishing this.

Automatic 

Both the culture and the UI skills are automatically set based on your visitor's browser settings. Try running the following page:


<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>CultureInfo demo</title>
</head>
<body>
    <form id="MainForm" runat="server">
        <% Response.Write("Your current culture: " + System.Globalization.CultureInfo.CurrentCulture.DisplayName); %>
    </form>
</body>
</html>

Now, access your browsers language settings: 

Internet Explorer: Click the Tools button, select Internet Options, then click the Languages button.

Firefox: Click the Tools menu, select Options, select the Content tab, and then click on the Select button in the language group.

Add another language, and then move it to the top of the list. Close the dialog and reload the page (F5). Now you will see the name of the culture that matches your chosen language.

The Page directive/class

As we saw in the previous chapter, we can simply define both Culture and UICulture by accessing the properties in the Page directive:


<%@ Page Language="C#" Culture="en-US" UICulture="en-US" %>

Since the Page Directive page is only a shortcut to the page class, it can also be done with CodeBehind. However, we have to do this definitely, before the page is presented, to ensure that it has the desired effect it is the place where the initial () method comes in the game, a method that is made by ASP.net It is said very quickly in the life cycle, which you can override:


public partial class CultureInfoTest : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        Page.Culture = "en-GB";
        Page.UICulture = "en-GB";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Page.Culture);
    }
}

You must know that Page.Culture and Page.UICulture are their own shortcuts for the system itself. threading. Thread.Centrethread.CurrentCulture and Systems threading. Thread.Centreaththread.Content Ukulcher

Globaly

You can set up culture and uicultures for all your pages through the web.config file. Use the globalization node, which is the system's web node. Web node, like this:


<globalization uiCulture="en-US" culture="en-US" />

If needed, you can still override this setting on individual pages.